<template>
  <!-- 管理员弹框 -->
  <a-modal
    :title="testPaperTitle"
    width="800px"
    centered
    :visible="testPaperVisible"
    :maskClosable="false"
    okText="确定"
    @ok="submitInform()"
    @cancel="closeDraw"
  >
    <div class="formDiv">
        <a-form layout="inline">
          <a-form-item label="试卷名称">
            <a-input v-model="name" type="text" style="width: 100px" />
          </a-form-item>
          <a-form-item label="试卷类型">
            <a-select v-model="type" style="width: 100px">
              <a-select-option v-for="type in typeData" :key="type">
                {{ type }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="站段">
            <a-select v-model="section" style="width: 100px">
              <a-select-option v-for="section in sectionData" :key="section">
                {{ section }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="search">搜索</a-button>
          </a-form-item>
          <a-form-item>
            <a-button @click="reset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>
    <div style="margin-top: 20px" class="table-content">
      <a-table
        bordered
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          type:'radio'
        }"
        :columns="columns"
        :data-source="list"
        rowKey="id"
        :pagination="pagination"
      >
      </a-table>
    </div>
  </a-modal>
</template>

<script>
import { paging } from "assets/js/mixin";
import { getTemplatesList,addTemplate,deleTemplate } from "@/api/templates";
import { getSectionArr } from "@/assets/js/utils";
import { mapMutations } from "vuex";
export default {
  mixins: [paging],
  props: {
    //关闭弹框
    closeFunc: {
      type: Function,
      default: () => {},
    },
    testPaperVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      name: "",
      id: "",
      type:"",
      section:"",
      typeData:["非正常行车"],
      sectionData:getSectionArr(),
      pagination: {
        //表格分页相关配置
        defaultPageSize: 5, 
        pageSizeOptions: ["5", "10"],
      },
      selectedRows: [],
      selectedRowKeys: [],
      columns: [
        {
          title: "试卷id",
          dataIndex: "id",
          scopedSlots: { customRender: "id" },
        },
        {
          title: "试卷名称",
          dataIndex: "name",
        },
        {
          title: "试卷类型",
          dataIndex: "type",
        },
        {
          title: "创建人",
          dataIndex: "publisherAccount",
        },
        {
          title: "站段",
          dataIndex: "section",
        },
        {
          title: "创建时间",
        }
      ],
      list:[],
      id: "",
      testPaperTitle: "添加试卷",
    };
  },

  methods: {
    ...mapMutations({
      setSelectRows: "SET_SELECT_ROWS",
    }),
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    submitInform() {
      this.setSelectRows(this.selectedRows);
      this.$emit("updateAddInform");
    },
    //搜索
    search() {
      this.pageNum = 1;
      this.pagination.current = 1;
      this.getTemplatesData();
    },
    //重置
    reset() {
      this.pageNum = 1;
      this.pagination.current = 1;
      this.section = "";
      this.name = "";
      this.type = "";
      this.getTemplatesData();
    },
    //获取试卷列表
    getTemplatesData(){
      this.loading = true;
      var params = {
        page: this.pageNum,
        limit: this.pageSize,
        section: this.section,
        name: this.name,
        type: this.type,
      };
      getTemplatesList(params)
        .then((res) => {
          this.list = res.data.papers;
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch((err) => {
          this.$message.destroy();
          this.$message.error(err);
        });
    },
    //关闭弹框
    closeDraw() {
      this.$emit("closeFunc");
    },
  },
  watch: {
    testPaperVisible(n) {
      if (n) {
        this.getTemplatesData()
        //this.selectedRows = this.$store.state.testSelectedRows
        this.selectedRowKeys=[]
        // if(this.selectedRows.length>0){
        //     this.selectedRows.forEach((item) => {
        //         this.selectedRowKeys.push(item.id)
        //     });
        // }
      } else {
      }
    },
  },
};
</script>

<style lang="stylus" scoped></style>